package com.shushan.shushanbao;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;

import com.android.log.CrashHandler;
import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.android.permission.rom.RomType;
import com.android.permission.rom.RomUtils;
import com.huawei.android.hms.agent.HMSAgent;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.nearme.game.sdk.GameCenterSDK;
import com.shushan.base.Constants;
import com.shushan.db.ChildBeanDao;
import com.shushan.db.UserBeanDao;
import com.shushan.login.bean.ChildBean;
import com.shushan.login.bean.UserBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.receiver.ScreenBroadcastReceiver;
import com.shushan.util.ControlUtil;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.SystemUtils;
import com.shushan.util.WxApiUtils;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.entity.UMessage;

import org.android.agoo.huawei.HuaWeiRegister;
import org.android.agoo.mezu.MeizuRegister;
import org.android.agoo.xiaomi.MiPushRegistar;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * @ClassName: BabyApplication
 * @Desciption: 整个应用的Application
 * @author: yichaohua
 * @date: 2018-05-30
 */
public class BabyApplication extends MultiDexApplication {

    private static final String TAG = "BabyApplication";

    private static BabyApplication babyApplication;

    private boolean isExaming = false;
    private boolean isHomeFresh = false;
    private ChildBean currentChildBean;
    private UserBean userBean;
    private ScreenBroadcastReceiver screenBroadcastReceiver = null;
    private UserChangeReceiver userChangeReceiver = null;
    private IWXAPI wxApi;
    private int znlx = 0;//我要玩手机是否有智能练习；
    private long screenOffTime = 0;

    public static BabyApplication getInstance() {
        return babyApplication;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        babyApplication = (BabyApplication) getApplicationContext();

        SharedPreferencesUtils.initSharedPreferences(this);

        if (BuildConfig.DEBUG) {
            //注册日志抓取
            CrashHandler.getInstance().init(this);
        }

        QbSdk.preInit(getInstance());//腾讯X5框架初始化

        initUeng();

        if(getPackageName().equals(Constants.HUAWEI_PACKAGE_NAME)){
            HMSAgent.init(this);
        }

        if("oppo".equals(BuildConfig.ENVIRONMENT)){
            //oppo联运SDK初始化
            GameCenterSDK.init(Constants.OPPO_APP_SECRET,this);
        }
        //注册微信
        wxApi = WxApiUtils.regToWx(getApplicationContext());

        //初始化语音评测服务
        SpeechUtility.createUtility(getApplicationContext(), SpeechConstant.APPID +"=5b358f1c");

        String processName = SystemUtils.getProcessName(this);
        if(!TextUtils.isEmpty(processName) && (processName.equals(Constants.PACKAGE_NAME) || processName.equals(Constants.HUAWEI_PACKAGE_NAME))){
            registerBroadcastReceiver();
            initOtherPush();
        }
    }

    private void initUeng() {
        //设置LOG开关，默认为false
        UMConfigure.setLogEnabled(true);
        //初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
        UMConfigure.init(this,UMConfigure.DEVICE_TYPE_PHONE,Constants.UMENG_MESSAGE_SECRET);

        final PushAgent mPushAgent = PushAgent.getInstance(this);
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                Log.i(TAG,"deviceToken=" + deviceToken);

                int currentChildId = getCurrentChildId();
                if(currentChildId > 0){
                    mPushAgent.setAlias(currentChildId+"", Constants.UMENG_ALIAS_CHILD_ID_KEY, new UTrack.ICallBack() {
                        @Override
                        public void onMessage(boolean isSuccess, String message) {
                            Log.i(TAG,"isSuccess=" + isSuccess + " message=" + message);
                        }
                    });
                }

                int useId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
                if(useId > 0){
                    mPushAgent.setAlias(useId+"", Constants.UMENG_ALIAS_USE_ID_KEY, new UTrack.ICallBack() {
                        @Override
                        public void onMessage(boolean isSuccess, String message) {
                            Log.i(TAG,"isSuccess=" + isSuccess + " message=" + message);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String s, String s1) {

            }
        });

        mPushAgent.setMessageHandler(messageHandler);

    }

    private void initOtherPush() {
        RomType romType = RomUtils.getRomType(this);
        if(romType == RomType.MIUI_ROM){
            MiPushRegistar.register(this, Constants.XIAOMI_PUSH_APP_ID, Constants.XIAOMI_PUSH_APP_KEY);
        }else if(romType == RomType.EMUI_ROM){
            HuaWeiRegister.register(this);
        }else if(romType == RomType.FLYME_ROM){
            MeizuRegister.register(this, Constants.FLYME_PUSH_APP_ID, Constants.FLYME_PUSH_APP_KEY);
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();

        if(screenBroadcastReceiver != null){
            unregisterReceiver(screenBroadcastReceiver);
        }

        if (userChangeReceiver != null){
            unregisterReceiver(userChangeReceiver);
        }

        if(getPackageName().equals(Constants.HUAWEI_PACKAGE_NAME)){
            HMSAgent.destroy();
        }
    }

    public UserBean getUserBean() {
        if(userBean != null){
            return userBean;
        }

        int currentUseId = getCurrentUseId();
        if(currentUseId <= 0){
            return null;
        }

        UserBeanDao userBeanDao = new UserBeanDao(this);
        UserBean userBean = userBeanDao.queryForId(currentUseId);

        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        if(userBean != null){
            this.userBean = userBean;

            SharedPreferencesUtils.setIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY,userBean.id);

            UserBeanDao userBeanDao = new UserBeanDao(this);
            if(userBeanDao.contain(userBean.id)){
                userBeanDao.update(userBean);
            }else {
                userBeanDao.add(userBean);
            }
        }

        if(currentChildBean != null){
            List<ChildBean> childList = userBean.getChildList();
            for (ChildBean childBean : childList){
                if (currentChildBean.id == childBean.id){
                    currentChildBean = childBean;
                    return;
                }
            }

            currentChildBean = null;
        }

        SharedPreferencesUtils.setIntDate(Constants.CURRENT_CHILD_ID_SHAREPREFERS_KEY,0);
    }

    public boolean isExaming() {
        return isExaming;
    }

    public void setExaming(boolean examing) {
        isExaming = examing;
    }

    public boolean isHomeFresh() {
        return isHomeFresh;
    }

    public void setHomeFresh(boolean homeFresh) {
        isHomeFresh = homeFresh;
    }

    public String getInviteCode(){
        if(userBean != null){
            return userBean.invite_code;
        }else {
            int currentUseId = getCurrentUseId();
            if(currentUseId <= 0){
                return "";
            }

            UserBeanDao userBeanDao = new UserBeanDao(this);
            UserBean userBean = userBeanDao.queryForId(currentUseId);
            return userBean.invite_code;
        }
    }

    public void setVip(boolean vip){
        if(userBean != null){
            userBean.vip = vip?1:0;
        }

        int currentUseId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        if(currentUseId <= 0){
            return;
        }

        UserBeanDao userBeanDao = new UserBeanDao(this);
        UserBean currentUseBean = userBeanDao.queryForId(currentUseId);
        if(currentUseBean == null){
            return;
        }

        currentUseBean.vip = vip?1:0;
        userBeanDao.update(currentUseBean);

    }

    public boolean isVip(){
        int currentUseId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        if(userBean == null && currentUseId <= 0){
            return false;
        }

        UserBean currentUseBean;
        if(userBean == null){
            currentUseBean = new UserBeanDao(this).queryForId(currentUseId);
        }else {
            currentUseBean = userBean;
        }

        if(currentUseBean == null){
            return false;
        }

        boolean isVip = currentUseBean.vip == 1 ? true : false;
        boolean isShareVip = currentUseBean.share_vip == 1 ? true : false;

        return (isVip || isShareVip);
    }

    public boolean isPayVip(){
        return (isOnlyVip() || isUseVip());
    }

    public boolean isOnlyVip(){
        int currentUseId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        if(userBean == null && currentUseId <= 0){
            return false;
        }

        UserBean currentUseBean;
        if(userBean == null){
            currentUseBean = new UserBeanDao(this).queryForId(currentUseId);
        }else {
            currentUseBean = userBean;
        }

        if(currentUseBean == null){
            return false;
        }

        boolean isVip = currentUseBean.vip == 1 ? true : false;

        return isVip;
    }

    public boolean isSectVip(int exerciseType){
        int currentUseId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        if(userBean == null && currentUseId <= 0){
            return false;
        }

        UserBean currentUseBean;
        if(userBean == null){
            currentUseBean = new UserBeanDao(this).queryForId(currentUseId);
        }else {
            currentUseBean = userBean;
        }

        if(currentUseBean == null){
            return false;
        }

        if(currentUseBean.vip == 1){
            return true;
        }

        boolean isSectVip = false;
        switch (exerciseType){
            case Constants.TYPE_SYNC_EXERCISE:
            case Constants.TYPE_LEARN_BY_DO:
                isSectVip = currentUseBean.tb_vip == 1 ? true : false;
                break;
            case Constants.TYPE_STRANGE_WORDS_EXERCISE:
                isSectVip = currentUseBean.sz_vip == 1 ? true : false;
                break;
            case Constants.TYPE_ORAL_CALCULATE_EXERCISE:
                isSectVip = currentUseBean.ks_vip == 1 ? true : false;
                break;
            case Constants.TYPE_SPEAKING_EXERCISE:
                isSectVip = currentUseBean.ky_vip == 1 ? true : false;
                break;
            case Constants.TYPE_ENGLISH_WORDS_EXERCISE:
                isSectVip = currentUseBean.dc_vip == 1 ? true : false;
                break;
            case Constants.TYPE_HEARING_EXERCISE:
                isSectVip = currentUseBean.tl_vip == 1 ? true : false;
                break;
            default:
                break;
        }

        return isSectVip;
    }

    public boolean isUseVip(){
        int currentUseId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        if(userBean == null && currentUseId <= 0){
            return false;
        }

        UserBean currentUseBean;
        if(userBean == null){
            currentUseBean = new UserBeanDao(this).queryForId(currentUseId);
        }else {
            currentUseBean = userBean;
        }

        if(currentUseBean == null){
            return false;
        }

        if(currentUseBean.vip == 1){
            return true;
        }

        int isVip = currentUseBean.tb_vip + currentUseBean.sz_vip + currentUseBean.ks_vip
                + currentUseBean.ky_vip + currentUseBean.dc_vip + currentUseBean.tl_vip;

        return (isVip>0)?true:false;
    }

    public boolean isChildNotSet(boolean isNotRewardSet){
        int currentChildId = getCurrentChildId();
        if(currentChildId <= 0){
            return true;
        }

        return isNotRewardSet;
    }

    public void updateStars(int starts){
        if(starts <= 0){
            return;
        }

        int currentChildId = getCurrentChildId();
        if(currentChildId <= 0){
            return;
        }

        UserBean currentUseBean;
        if(userBean == null){
            int currentUseId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
            if(currentUseId <= 0){
                return;
            }else {
                currentUseBean = new UserBeanDao(this).queryForId(currentUseId);
            }

        }else {
            currentUseBean = userBean;
        }

        if(currentUseBean != null){
            List<ChildBean> childList = currentUseBean.getChildList();
            for (ChildBean childBean : childList){
                if(childBean.id == currentChildId){
                    childBean.stars += starts;
                    break;
                }
            }
        }

        new ChildBeanDao(this).updateStarts(currentChildId,starts);
    }

    public List<ChildBean> getChildBeanList() {
        List<ChildBean> childList = null;

        if (userBean != null) {
            childList = userBean.getChildList();
        } else {
            ChildBeanDao childBeanDao = new ChildBeanDao(this);
            childList = childBeanDao.queryForAll();
        }

        return childList;
    }

    public List<ChildBean> getAllChild() {
        if (userBean != null) {
            return userBean.getChildList();
        } else {
            return new ChildBeanDao(this).queryForAll();
        }

    }

    public ChildBean getChildBean(int childId) {
        if (childId < 0) {
            return null;
        }

        List<ChildBean> childList;

        List<ChildBean> childBeanList = getChildBeanList();
        if (childBeanList == null || childBeanList.size() == 0) {
            childList = new ChildBeanDao(this).queryForAll();
        } else {
            childList = childBeanList;
        }

        if (childList == null) {
            return null;
        }

        for (ChildBean childBean : childList) {
            if (childBean.id == childId) {
                return childBean;
            }

        }

        return null;
    }

    public ChildBean getChildBean(String passWd) {
        if (TextUtils.isEmpty(passWd)) {
            return null;
        }

        List<ChildBean> childList;

        List<ChildBean> childBeanList = getChildBeanList();
        if (childBeanList == null || childBeanList.size() == 0) {
            childList = new ChildBeanDao(this).queryForAll();
        } else {
            childList = childBeanList;
        }

        if (childList == null) {
            return null;
        }

        for (ChildBean childBean : childList) {
            if (childBean.childPassword.equals(passWd)) {
                return childBean;
            }

        }

        return null;
    }

    public void setCurrentChildBean(ChildBean currentChildBean) {
        this.currentChildBean = restoreChildBean(currentChildBean);
    }

    public void setCurrentChildBean(String passWd) {
        if (TextUtils.isEmpty(passWd)) {
            return;
        }

        ChildBean childBean = getChildBean(passWd);
        if (childBean == null) {
            return;
        } else {
            this.currentChildBean = restoreChildBean(childBean);
        }

    }

    public int getCurrentUseId() {
        int currentUseId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        Log.i(TAG,"getCurrentUseId currentUseId=" + currentUseId);
        if (currentUseId > 0) {
            return currentUseId;
        } else if(userBean != null){
            return userBean.id;
        } else {
            List<UserBean> userBeanList = new UserBeanDao(this).queryForAll();
            if (userBeanList != null && userBeanList.size() > 0) {
                return userBeanList.get(0).id;
            }
        }

        return -1;
    }

    public int getCurrentChildId() {
        int currentChildId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_CHILD_ID_SHAREPREFERS_KEY);
        Log.i(TAG,"getCurrentChildId currentChildId=" + currentChildId);
        if (currentChildId > 0) {
            return currentChildId;
        } else {
            ChildBean childBean = getCurrentChildBean();
            if (childBean != null) {
                return childBean.id;
            }
        }

        return -1;
    }

    public ChildBean getCurrentChildBean() {
        if (currentChildBean != null) {
            return currentChildBean;
        }

        int currentChildId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_CHILD_ID_SHAREPREFERS_KEY);
        List<ChildBean> childBeanList = getChildBeanList();
        if (currentChildId > 0) {
            if (childBeanList != null && childBeanList.size() > 0) {
                for (ChildBean childBean : childBeanList) {
                    if (childBean.id == currentChildId) {
                        return childBean;
                    }
                }

                return childBeanList.get(0);
            } else {
                ChildBeanDao childBeanDao = new ChildBeanDao(this);
                ChildBean childBean = childBeanDao.queryForId(currentChildId);
                if (childBean != null) {
                    return childBean;
                }

                return null;
            }
        } else {
            if (childBeanList != null && childBeanList.size() > 0) {
                return childBeanList.get(0);
            } else {
                ChildBeanDao childBeanDao = new ChildBeanDao(this);
                List<ChildBean> childList = childBeanDao.queryForAll();
                if (childList != null && childList.size() > 0) {
                    return childList.get(0);
                } else {
                    return null;
                }

            }
        }

    }

    public ChildBean getCurrentChildRestore() {
        if (currentChildBean != null) {
            return restoreChildBean(currentChildBean);
        }

        int currentChildId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_CHILD_ID_SHAREPREFERS_KEY);
        List<ChildBean> childBeanList = getChildBeanList();
        if (currentChildId > 0) {
            if (childBeanList != null && childBeanList.size() > 0) {
                for (ChildBean childBean : childBeanList) {
                    if (childBean.id == currentChildId) {
                        return restoreChildBean(childBean);
                    }
                }

                return restoreChildBean(childBeanList.get(0));
            } else {
                ChildBeanDao childBeanDao = new ChildBeanDao(this);
                ChildBean childBean = childBeanDao.queryForId(currentChildId);
                if (childBean != null) {
                    return restoreChildBean(childBean);
                }

                return null;
            }
        } else {
            if (childBeanList != null && childBeanList.size() > 0) {
                return restoreChildBean(childBeanList.get(0));
            } else {
                ChildBeanDao childBeanDao = new ChildBeanDao(this);
                List<ChildBean> childList = childBeanDao.queryForAll();
                if (childList != null && childList.size() > 0) {
                    return restoreChildBean(childList.get(0));
                } else {
                    return null;
                }

            }
        }

    }

    private ChildBean restoreChildBean(ChildBean childBean) {
        if (childBean == null) {
            return null;
        }

        SharedPreferencesUtils.setIntDate(Constants.CURRENT_CHILD_ID_SHAREPREFERS_KEY, childBean.id);

        return childBean;
    }


    public boolean isChildPasswd(String passWd) {
        if (TextUtils.isEmpty(passWd)) {
            return false;
        }

        List<ChildBean> childList;

        List<ChildBean> childBeanList = getChildBeanList();
        if (childBeanList == null || childBeanList.size() == 0) {
            childList = new ChildBeanDao(this).queryForAll();
        } else {
            childList = childBeanList;
        }

        if (childList == null) {
            return false;
        }

        for (ChildBean childBean : childList) {
            if (childBean.childPassword.equals(passWd)) {
                return true;
            }
        }

        return false;
    }

    public void chilidAdd(ChildBean childBean) {
        if (childBean == null) {
            return;
        }

        childApplicationAdd(childBean);
        childDBAdd(childBean);
    }

    private void childApplicationAdd(ChildBean childBean) {
        List<ChildBean> childBeanList = getChildBeanList();
        if (childBean == null || childBeanList == null) {
            return;
        }

        for (ChildBean childBean1 : childBeanList) {
            if (childBean1.id == childBean.id) {
                childBeanList.remove(childBean1);
                childBeanList.add(childBean);
                return;
            }
        }

        childBeanList.add(childBean);
    }

    private void childDBAdd(ChildBean childBean) {
        if (childBean == null) {
            return;
        }

        ChildBeanDao childBeanDao = new ChildBeanDao(this);
        List<ChildBean> childList = childBeanDao.queryForAll();
        if (childList == null) {
            return;
        }

        for (ChildBean childBean1 : childList) {
            if (childBean1.id == childBean.id) {
                childBeanDao.update(childBean);
                return;
            }
        }

        childBeanDao.add(childBean);
    }

    public void chilidDelete(ChildBean childBean) {
        if (childBean == null) {
            return;
        }

        childApplicationDelete(childBean);
        childDBDelete(childBean);
    }

    private void childApplicationDelete(ChildBean childBean) {
        List<ChildBean> childBeanList = getChildBeanList();
        if (childBean == null || childBeanList == null || childBeanList.size() == 0) {
            return;
        }

        for (ChildBean childBean1 : childBeanList) {
            if (childBean1.id == childBean.id) {
                childBeanList.remove(childBean);
                return;
            }
        }

    }

    private void childDBDelete(ChildBean childBean) {
        if (childBean == null) {
            return;
        }

        ChildBeanDao childBeanDao = new ChildBeanDao(this);
        List<ChildBean> childList = childBeanDao.queryForAll();
        if (childList == null || childList.size() == 0) {
            return;
        }

        for (ChildBean childBean1 : childList) {
            if (childBean1.id == childBean.id) {
                childBeanDao.delete(childBean);
            }
        }

    }

    public int getZnlx() {
        return znlx;
    }

    public void setZnlx(int znlx) {
        this.znlx = znlx;
    }

    public long getScreenOffTime() {
        return screenOffTime;
    }

    public void setScreenOffTime(long screenOffTime) {
        this.screenOffTime = screenOffTime;
    }

    public boolean hasSamePasswd(String passWd) {
        if (TextUtils.isEmpty(passWd)) {
            return false;
        }

        List<ChildBean> childBeanList = getChildBeanList();
        if (childBeanList != null && childBeanList.size() > 0) {
            for (ChildBean childBean : childBeanList) {
                if (childBean.childPassword.equals(passWd)) {
                    return true;
                }
            }

        }

        return false;
    }

    public boolean hasSameChildName(String name) {
        if (TextUtils.isEmpty(name)) {
            return false;
        }

        List<ChildBean> childBeanList = getChildBeanList();
        if (childBeanList != null && childBeanList.size() > 0) {
            for (ChildBean childBean : childBeanList) {
                if (childBean.childName.equals(name)) {
                    return true;
                }
            }

        }

        return false;
    }

    private void registerBroadcastReceiver() {
        screenBroadcastReceiver = new ScreenBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);//当屏幕点时候触发
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);//当屏幕灭的时候触发
        intentFilter.addAction(Intent.ACTION_USER_PRESENT);//当用户解锁时触发
        registerReceiver(screenBroadcastReceiver, intentFilter);

        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        userChangeReceiver = new UserChangeReceiver();
        IntentFilter intentFilter2 = new IntentFilter();
        intentFilter2.addAction(Constants.USER_CHANGE_RECEIVER_ACTION);
        localBroadcastManager.registerReceiver(userChangeReceiver, intentFilter2);
    }


    private class UserChangeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG,"UserChangeReceiver onReceive()");

            getUserinfo(context);
        }
    }

    public void getUserinfo(final Context context) {
        UrlParse urlParse = new UrlCommParse(context, UrlConst.BASE_URL).appendRegion(UrlConst.GET_USER_INFO);
        urlParse.putValue("phone",SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY));

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<UserBean>() {


                    @Override
                    protected void onSuccess(UserBean userBean) {
                        if(userBean != null){
                            setUserBean(userBean);

                            if(!TextUtils.isEmpty(userBean.password)){
                                SharedPreferencesUtils.setStringDate(Constants.PARENT_PASSWD_SHAREPREFERS_KEY,userBean.password);
                            }

                            List<ChildBean> childList = userBean.getChildList();
                            if(childList.size() > 0){
                                new ChildBeanDao(babyApplication).dropAndAddList(childList);
                            }
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {

                    }
                });
    }


    public IWXAPI getWxApi() {
        return wxApi;
    }

    @SuppressLint("HandlerLeak")
    private final static Handler dealCustomHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            int time = msg.arg1;
            String tempPsw = (String) msg.obj;

            switch (msg.what){
                case 1:
                    ControlUtil.lockChildPsw(getInstance(),time);
                    break;
                case 2:
                    ControlUtil.freeGame(getInstance(),tempPsw,time);
                    break;
                case 3:
                    ControlUtil.unLockChildPsw(getInstance(),time);
                    break;
                case 4:
                    ControlUtil.lockFreeGame(getInstance(),tempPsw,time);
                    break;
                default:
                    break;
            }
        }
    };

    private UmengMessageHandler messageHandler = new UmengMessageHandler() {

        @Override
        public void dealWithCustomMessage(Context context, UMessage uMessage) {
            super.dealWithCustomMessage(context, uMessage);

            String custom = uMessage.custom;
            Log.i(TAG,"custom=" + custom);

            if(!TextUtils.isEmpty(custom)){
                try {
                    JSONObject jsonObject = new JSONObject(custom);
                    int pushType = jsonObject.optInt("pushType",0);
                    int time = jsonObject.optInt("time",0);
                    String tempPsw = jsonObject.optString("tempPsw","");

                    switch (pushType){
                        case 1:
                            Message message = dealCustomHandler.obtainMessage(1);
                            message.arg1 = time;
                            dealCustomHandler.sendMessage(message);
                            break;
                        case 2:
                            Message message1 = dealCustomHandler.obtainMessage(2);
                            message1.arg1 = time;
                            message1.obj = tempPsw;
                            dealCustomHandler.sendMessage(message1);
                            break;
                        case 3:
                            Message message2 = dealCustomHandler.obtainMessage(3);
                            message2.arg1 = time;
                            dealCustomHandler.sendMessage(message2);
                            break;
                        case 4:
                            Message message3 = dealCustomHandler.obtainMessage(4);
                            message3.arg1 = time;
                            message3.obj = tempPsw;
                            dealCustomHandler.sendMessage(message3);
                            break;
                        default:
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}
